<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      id="systemAppVersion"
      v-model:columns="columns"
      :loading="loading"
      :data="tableData"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="false"
      @refresh="getTableData"
    >
      <template #action="{ record }">
        <a-button type="text" size="small" @click="onAddOrEdit(record)"> 编辑 </a-button>
      </template>
    </GiTable>
    <EditDrawer ref="EditDrawerRef" @getTableData="getTableData"></EditDrawer>
  </div>
</template>

<script setup>
import { appVersionConfigList } from '@/apis'
import EditDrawer from './EditDrawer.vue'

defineOptions({ name: 'systemAppVersion' })

const columns = ref([
  {
    title: '标题',
    width: 100,
    dataIndex: 'title'
  },
  {
    title: '描述',
    width: 250,
    tooltip: true,
    ellipsis: true,
    dataIndex: 'desc'
  },
  {
    title: '系统',
    width: 80,
    render: (data) => {
      const text = data.record.os
      return text == 1 ? '安卓' : '苹果'
    }
  },
  {
    title: '版本号',
    width: 100,
    dataIndex: 'versionNumber'
  },
  {
    title: '版本名称',
    width: 100,
    dataIndex: 'versionName'
  },
  {
    title: '会员中心版本号',
    width: 150,
    render: (data) => {
      const text = data.record.userVersionNumber
      return text ? text : '--'
    }
  },
  {
    title: '下载地址',
    width: 150,
    tooltip: true,
    ellipsis: true,
    dataIndex: 'downloadLink'
  },
  {
    title: '版本类型',
    width: 120,
    render: (data) => {
      const text = data.record.type
      return text == 0 ? '升级' : '内测'
    }
  },
  {
    title: '间隔时间',
    width: 120,
    dataIndex: 'interval'
  },
  {
    title: '是否强制更新',
    width: 140,
    render: (data) => {
      const text = data.record.isForce
      return text == 1 ? '是' : '否'
    }
  },
  {
    title: '操作',
    width: 80,
    fixed: 'right',
    align: 'center',
    slotName: 'action'
  }
])

const EditDrawerRef = ref()
const loading = ref(false)
const tableData = ref([])

const listParams = reactive({
  orderBy: undefined,
  sort: undefined,
  keyword: undefined,
  categoryId: undefined,
  buyerCircleAuthorId: undefined,
  goodsId: undefined,
  date: undefined,
  pid: 10
})

// 获取列表数据
const getTableData = async () => {
  try {
    loading.value = true
    const res = await appVersionConfigList(listParams)
    tableData.value = res.data.list
  } finally {
    loading.value = false
  }
}
getTableData()

// 添加/编辑
const onAddOrEdit = (item) => {
  EditDrawerRef.value?.addOrEdit(item)
}
</script>
